<?php include("../config.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $sitename; ?> - Sysop: PAD List Backup</title>
<meta name="description" content="<?php echo $sitename; ?> - Sysop: PAD List Backup">
<meta name="keywords" content="free to try, software, shareware, freeware, developers, vendors, download, PAD">
<meta name="author" content="Andrey Aleksanyants, M.D.">
<meta name="copyright" content="easy HTools">
<meta http-equiv="imagetoolbar" content="no">
<link rel="stylesheet" type="text/css" href="../<?php echo $cssfile; ?>">
<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
<table class="tmain" align="center" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td class="tmain">
<table class="tlogo" align="center" width="100%" cellspacing="0" cellpadding="4">
<tr>
<td align="right">
<p><a href="index.php">Home</a></p>
</td>
</tr>
</table>
</td>
</tr><tr>
<td class="tmain">
<br>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="1">
<tr valign="top">
<td class="tbody">
<h1><?php echo $sitename; ?> - Sysop: PAD List Backup</h1>
<?php

// Requesting form data
$category = $_POST["category"];
$sbutton  = $_POST["sbutton"];

if(!($link_id = mysql_connect($dbhost, $userid, $userpassword))) {
	die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
}
mysql_select_db($dbname);

if(empty($sbutton)) {
	$result         = mysql_query("SELECT * FROM paddata", $link_id);
	$numrows        = mysql_num_rows($result);
	echo "<p>There are <b>" . $numrows . "</b> PADs listed in your database.</p><br>\n";
	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<table class=\"tvisible\" align=\"center\" cellspacing=\"0\" cellpadding=\"4\">\n";
	echo "<tr>\n";
	echo "<th class=\"tablehead\" colspan=\"2\">Save PAD List</th>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\"><p><b>Select category:</b></p></td>\n";
	echo "<td><select name=\"category\" id=\"category\" size=\"1\">\n";
	echo "<option value=\"All\">All</option>\n";
	$catfile = "../categories.txt";
	if(file_exists($catfile)) {
		$catary = file($catfile);
		for($i=0; $i<count($catary); $i++) {
			$cdisp = rtrim($catary[$i]);
			echo "<option value=\"" . $cdisp . "\">" . $cdisp . "</option>\n";
		}
	}
	echo "</select></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\"><p><b>Save list:</b></p></td>\n";
	echo "<td><input type=\"submit\" class=\"button\" name=\"sbutton\" value=\"  Save  \"></td>\n";
	echo "</tr>\n</table>\n</form>\n<br>\n";
}
else {
	if($category == "All") {
		$sql = "SELECT padfile FROM paddata ORDER BY padfile";
	}
	else {
		$sql = "SELECT padfile FROM paddata WHERE category LIKE '$category%' OR category LIKE '" . str_replace(" &amp; "," & ",$category) . "%' ORDER BY padfile";
	}
	$result = mysql_query($sql, $link_id);
	$numrows = 0;
	if($result) $numrows = mysql_num_rows($result);
	echo "<p>There are <b>" . $numrows . "</b> PADs listed in category \"" . htmlspecialchars($category) . "\". Saving list...</p><br>\n";

	$padlist = "padlist.txt";
	$fp = fopen($padlist,"w");
	for($i=0; $i<$numrows; $i++) {
		$query = mysql_result($result, $i);
		fwrite($fp,$query . "\n");
	}
	fclose($fp);
	chmod($padlist,0777);
	
	echo "<p>Download <a href=\"" . $padlist . "\">" . $padlist . "</a> file (" . filesize($padlist) . " Bytes) to your local drive.</p><br>\n";
}
mysql_close($link_id);
?>
<br>

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>